package com.fendaren.controller.intercept;

import com.fendaren.base.BaseInfoProperties;
import com.fendaren.exceptions.GraceException;
import com.fendaren.exceptions.MyCustomException;
import com.fendaren.grace.result.ResponseStatusEnum;
import com.fendaren.util.IPUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SMSInterceptor extends BaseInfoProperties implements HandlerInterceptor {

    private static final Logger log = LoggerFactory.getLogger(SMSInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String userIp = IPUtil.getRequestIp(request);
        // 获得用于判断的boolean
        boolean ipExist = redis.keyIsExist(MOBILE_SMSCODE + ":" + userIp);
        if (ipExist) {
            log.info("短信发送频率太高了");
            GraceException.display(ResponseStatusEnum.SMS_NEED_WAIT_ERROR);
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
